Skip to content

Add Grad-CAM interpretability method#887

Open
JunboShen wants to merge 1 commit intosunlabuiuc:masterfrom
JunboShen:gradcam
Open

Add Grad-CAM interpretability method#887
JunboShen wants to merge 1 commit intosunlabuiuc:masterfrom
JunboShen:gradcam

Conversation

@JunboShen
Copy link

Contributor: Junbo Shen
Contribution Type: Interpretability method, tests, example

Description

Add GradCAM to pyhealth.interpret.methods for CNN-based medical
image classification.

Main behavior:

  • supports target_layer as either an nn.Module or a dotted path such as
    model.layer4.1.conv2
  • returns image heatmaps as {input_key: cam} with shape [B, H, W]
  • uses the predicted class by default when class_index is not provided
  • uses logit when available and falls back to y_prob otherwise

And add:

  • chest X-ray tutorial script using Grad-CAM; API docs for Grad-CAM; unit tests for Grad-CAM

Files added/changed

  1. pyhealth/interpret/methods/gradcam.py
  2. tests/core/test_gradcam.py
  3. examples/cxr/gradcam_cxr_tutorial.py
  4. docs/api/interpret.rst
  5. docs/api/interpret/pyhealth.interpret.methods.gradcam.rst
  6. docs/tutorials.rst

Testing

Validated locally with:

python -m py_compile \
  pyhealth/interpret/methods/gradcam.py \
  tests/core/test_gradcam.py \
  examples/cxr/gradcam_cxr_tutorial.py

python -m unittest tests.core.test_gradcam -v

python examples/cxr/gradcam_cxr_tutorial.py \
  --root /Users/junboshen/Documents/COVID-19_Radiography_Dataset \
  --weights none \
  --output /tmp/gradcam_cxr_overlay.png

The Grad-CAM implementation was also checked against the original
jacobgil/pytorch-grad-cam implementation by comparing the output.

Copy link
Collaborator

@jhnwu3 jhnwu3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WIll take a deeper look later.

But, @JunboShen if you're not already in the discord, would love to have you join the interpretability team.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants